package com.jar.exercise.算法.数组的反转;

/**
 * 数组反转
 */
public class Demo {
	public static void main(String[] args) {
       /* StringBuilder s = new StringBuilder("abcdefg");
        StringBuilder reverse1 = s.reverse();
        System.out.println(reverse1);*/
		int[] arr = new int[]{12, 32, 56, 89, 12, 32, -45, 0, 5};
		int[] reverse = reverse2(arr);
		for (int i : reverse) {
			System.out.print(i + ",");
		}
	}

	/**
	 * 方法一
	 */
	private static int[] reverse(int[] arr) {
		for (int i = 0; i < arr.length / 2; i++) {
			int temp = arr[i];
			arr[i] = arr[arr.length - i - 1];
			arr[arr.length - i - 1] = temp;
		}
		return arr;
	}

	/**
	 * 方法2
	 */
	private static int[] reverse2(int[] arr) {
		for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
			int temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
		return arr;
	}
}
